package core.inputoutput.parsers;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Observable;
import java.util.Scanner;

import core.inputoutput.formatting.Format;

/**
 * Generic PDBParser, sets the framework for parseing a file in any pdb format. Built on the idea 
 * of each PDB line being formatted by column width.
 * @author Julian
 *
 * @param <T>
 */
public abstract class PDBParser<T> extends Observable implements IParser<T>{

	@Override
	public void notifyObservers() {
		this.setChanged();
		super.notifyObservers();
	}


	protected int totalLines;
	@Override
	public T parse(File file, Format format) {
		
		try {
			totalLines = findNumLines(new Scanner(file));
			System.out.println("done with lines");
			return parse(new Scanner(file), format);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	public abstract T parse(Scanner scanner, Format format);

	
	/**
	 * Finds the number of lines in the scanned PDB document and resets the scanner
	 * @param scan
	 * @return
	 */
	protected int findNumLines(Scanner scan) {
		
		int lines = 0;
		while (scan.hasNextLine() && scan.nextLine().length() > 0){
			System.out.println(lines);
			lines++;
		}
		
		return lines;
		
	}
	
}
